package com.as.api;

import com.alibaba.fastjson.JSONObject;
import com.as.base.ResultBean;
import com.as.entity.Account;
import com.as.entity.Stream;
import com.as.entity.TypeIn;
import com.as.service.AccountService;
import com.as.service.StreamService;
import com.as.service.TypeInService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 账单流水API
 *
 * @author YeJianFeng
 */
@RestController
@RequestMapping("/stream")
@CrossOrigin
@Transactional
public class StreamApi {

    @Autowired
    private StreamService streamService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private TypeInService typeInService;

    @GetMapping("/findAll")
    public ResultBean<PageInfo<Stream>> findAll(@RequestParam("page") Integer page,
                                                @RequestParam("size") Integer size,
                                                @RequestParam("timeYue") String timeYue) {
        PageHelper.startPage(page, size);
//        List<Stream> streams = streamService.queryAll();
        List<Stream> streams;
        Map<String, Object> map = new HashMap<>();
        if (timeYue != null && timeYue.length() > 0) {
            map.put("date", timeYue);
            streams = streamService.queryByLike(Stream.class, map, null);
        } else {
            streams = streamService.queryAll();
        }
//        List<Stream> streams = streamService.queryByDate(startTime, endTime);
        return new ResultBean<>(new PageInfo<>(streams));
    }

    @GetMapping("/findTypes")
    public ResultBean<List<TypeIn>> findTypes() {
        List<TypeIn> types = typeInService.queryAll();
        return new ResultBean<>(types);
    }

    @PostMapping("/update")
    public ResultBean<Stream> update(@RequestBody Stream newStream) {
        // 根据原流水记录恢复账单金额
        Stream oldStream = newStream.getOldStream();
        Account oldAccount = accountService.queryById(oldStream.getAcc());
        BigDecimal oldAccountBalance = oldAccount.getBalance();
        BigDecimal oldStreamMoney = oldStream.getMoney();
        oldAccount.setBalance(oldAccountBalance.subtract(oldStreamMoney));
        accountService.updateSelective(oldAccount);
        // 根据新流水记录 记录新的账单金额
        Account newAccount = accountService.queryById(newStream.getAcc());
        BigDecimal newAccountBalance = newAccount.getBalance();
        BigDecimal newStreamMoney = newStream.getMoney();
        newAccount.setBalance(newAccountBalance.add(newStreamMoney));
        accountService.updateSelective(newAccount);
        streamService.updateSelective(newStream);
        newStream.setAccountMain(newAccount);
        return new ResultBean<>(newStream);
    }

    @PostMapping("/addAcc")
    public ResultBean<Account> addAcc(@RequestBody Stream stream) {
        streamService.save(stream);
        Account account = accountService.queryById(stream.getAcc());
        BigDecimal balance = account.getBalance();
        BigDecimal resultMoney = balance.add(stream.getMoney());
        account.setBalance(resultMoney);
        accountService.updateSelective(account);
        return new ResultBean<>(account);
    }

    @PostMapping("/delIds")
    public ResultBean<String> delIds(@RequestBody List<JSONObject> params){
        for (JSONObject param : params) {
            Account account = accountService.queryById(param.getInteger("acc"));
            BigDecimal balance = account.getBalance();
            BigDecimal newStreamMoney = param.getBigDecimal("money");
            BigDecimal resultMoney = balance.subtract(newStreamMoney);
            account.setBalance(resultMoney);
            accountService.updateSelective(account);
            Integer streamId = param.getInteger("id");
            streamService.deleteById(streamId);
        }
        return new ResultBean<>("删除成功!");
    }
}
